﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using WFNetLib.MyControls;

namespace 自消费
{
    public partial class 日志 : Form
    {
        public 日志()
        {
            InitializeComponent();
        }

        private void 日志_Load(object sender, EventArgs e)
        {
            WFNetLib.MyControls.ToolStripDateTimePicker startDateTimePicker = new WFNetLib.MyControls.ToolStripDateTimePicker(DateTime.Now.AddDays(-7));
            startDateTimePicker.Control.Width = 120;
            toolStrip1.Items.Insert(7, startDateTimePicker);
            WFNetLib.MyControls.ToolStripDateTimePicker endDateTimePicker = new WFNetLib.MyControls.ToolStripDateTimePicker(DateTime.Now);
            endDateTimePicker.Control.Width = 120;
            toolStrip1.Items.Insert(9, endDateTimePicker);            
            OleDbDataReader reader = CardTaskDataDBOption.ReaderSelect(null);
            List<string> taskNameList = new List<string>();
            List<ComboBoxItem> taskTypeList = new List<ComboBoxItem>();
            bool bExit = false;
            while (reader.Read())
            {
                CardTaskData ctd = CardTaskDataFactory.Construct(reader);
                bExit = false;
                foreach (string taskname in taskNameList)
                {
                    if (taskname == ctd.task_name)
                    {
                        bExit = true;
                        break;
                    }
                }
                if (!bExit)
                    taskNameList.Add(ctd.task_name);
                bExit = false;
                foreach (ComboBoxItem group in taskTypeList)
                {
                    if (Convert.ToInt32(group.Value) == ctd.task_type)
                    {
                        bExit = true;
                        break;
                    }
                }
                if (!bExit)
                {
                    ComboBoxItem group;
                    switch (ctd.task_type)
                    {
                        case 1:
                            group = new ComboBoxItem("主题", ctd.task_type);
                            break;
                        default:
                            group = new ComboBoxItem("未知", ctd.task_type);
                            break;
                    }                    
                    taskTypeList.Add(group);
                }
            }
            reader.Close();
            toolStripComboBox1.Items.Add("全部");
            toolStripComboBox2.Items.Add("全部");
            toolStripComboBox1.Items.AddRange(taskNameList.ToArray());
            toolStripComboBox2.Items.AddRange(taskTypeList.ToArray());
            toolStripComboBox1.SelectedIndex = 0;
            toolStripComboBox2.SelectedIndex = 0;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (toolStripButton1.Text == "成功")
                toolStripButton1.Text = "失败";
            else
                toolStripButton1.Text = "成功";
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string sql;
            sql = "select * from CardTask where ";
            if (toolStripComboBox1.SelectedIndex != 0)
            {
                sql = sql + "task_name='" + toolStripComboBox1.Text + "' And ";
            }
            if (toolStripComboBox2.SelectedIndex != 0)
            {
                ComboBoxItem group = (ComboBoxItem)toolStripComboBox2.SelectedItem;
                sql = sql + "task_type=" + Convert.ToInt32(group.Value) + " And ";
            }
            if(toolStripButton1.Text=="成功")
                sql = sql + "succeed=true And ";
            else
                sql = sql + "succeed=false And ";
            DateTime start, end;
            start = ((WFNetLib.MyControls.ToolStripDateTimePicker)toolStrip1.Items[7]).Control.Value;
            end = ((WFNetLib.MyControls.ToolStripDateTimePicker)toolStrip1.Items[9]).Control.Value;
            sql = sql + "starttime BETWEEN #" + start.ToShortDateString() + "# And #" + end.AddDays(1).ToShortDateString() + "#";
            sql = sql + " order by starttime";
            OleDbDataReader reader = WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql);
            listView1.Items.Clear();
            while (reader.Read())
            {
                CardTaskData ctd = CardTaskDataFactory.Construct(reader);
                ListViewItem lvi = listView1.Items.Add(ctd.card_id);
                lvi.Tag = ctd.ID;
                lvi.SubItems.Add("COM" + ctd.commport.ToString());
                switch (ctd.task_type)
                {
                    case 1:
                        lvi.SubItems.Add("主题");
                        break;
                    default:
                        lvi.SubItems.Add("未知");
                        break;
                }
                lvi.SubItems.Add(ctd.task_name);
                lvi.SubItems.Add(ctd.fail_reason);
                lvi.SubItems.Add(ctd.exec_result);
                lvi.SubItems.Add(ctd.starttime.ToString());
                lvi.SubItems.Add(ctd.endtime.ToString());
                lvi.SubItems.Add(ctd.task_content);
                lvi.SubItems.Add(ctd.imei);
            }
            reader.Close();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否删除当前所有查询出的日志信息，删除后无法恢复", "警告", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
            string sql;
            foreach (ListViewItem lvi in listView1.Items)
            {
                sql = "delete from CardTask where ID=" + lvi.Tag.ToString();
                WFNetLib.ADO.AccessOP.NonQuerySQL(sql);
            }
            listView1.Items.Clear();
        }
    }
}
